package com.market.plus.controller;

import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.market.plus.controller.mapper.JsonMapping;
import com.market.plus.controller.model.TribeInstance;
import com.market.plus.controller.validator.TribeValidator;
import com.market.plus.core.dto.TribeDto;
import com.market.plus.core.service.TribeService;

@Controller
public class TribeController {

   public static final String CREATE_PAGE = "tribe/create";

   public static final String STATUS_PAGE = "tribe/statusHTML";

   public static final String TRIBES_PAGE = "tribe/tribes";

   private TribeService tribeService;
   
   private TribeValidator tribeValidator;
   
   private JsonMapping jsonMapping;
   
   @Autowired
   public TribeController(@Qualifier("tribeService") TribeService tribeService,
         @Qualifier("jsonMapping") JsonMapping jsonMapping,
         @Qualifier("tribeValidator")TribeValidator tribeValidator) {
      this.tribeService=tribeService;
      this.jsonMapping=jsonMapping;
      this.tribeValidator=tribeValidator;
   }

   @RequestMapping(value = "/participant/{participantId}/tribe", method = RequestMethod.GET)
   public String viewCreateForm(@PathVariable("participantId") Long participantId,ModelMap model) {
      model.addAttribute("participantId", participantId);
      return CREATE_PAGE;
   }
   
   @RequestMapping(value = "/participant/{participantId}/tribe", method = RequestMethod.POST)
   public String create(@PathVariable("participantId") Long participantId, @ModelAttribute("tribeIntance") TribeInstance tribeIntance, BindingResult result, ModelMap model) throws Exception {
      this.tribeValidator.validate(tribeIntance.getTribe(), result);
      if (!result.hasErrors()){
         String guid=this.tribeService.saveTribe(tribeIntance.getTribe(), tribeIntance.getFileData(), participantId);
         model.addAttribute("guid", guid);
      }
      return STATUS_PAGE;
   }

   @RequestMapping(value = "/participant/{participantGUID}/tribe/list", method = RequestMethod.GET)
   public String listTribes(@PathVariable("participantGUID") String participantGUID,ModelMap model){
      List<TribeDto> tribeDtos = this.tribeService.getTribes(participantGUID);
      model.addAttribute("tribes", tribeDtos);
      return TRIBES_PAGE;
   }

   @RequestMapping(value = "/tribe/{tribeGUID}/news", method = RequestMethod.GET)
   public @ResponseBody JSONObject getTribe(@PathVariable("tribeGUID") String tribeGUID) throws Exception {
      TribeDto tribeDto = this.tribeService.getNews(tribeGUID);
      JSONObject tribeObject = this.jsonMapping.convertFirtsTribeNews(tribeDto);
      return tribeObject;
   }
}
